﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;

using Warehouse.UI.WinForm.Properties;
using Warehouse.UI.WinForm.Forms;

namespace Warehouse.UI.WinForm
{
    public partial class MainForm : Form
    {
        private void _initCreate()
        {
            this.Text = Resources.ProgramName;
        }

        //--Constructor
        public MainForm()
        {
            InitializeComponent();
            _initCreate();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            //
        }

        private void frmMain_Shown(object sender, EventArgs e)
        {
            _showLoginForm();
        }

        private void miClose_Click(object sender, EventArgs e)
        {
            _closeAllForm();
            this.Close();
        }

        private void miLogin_Click(object sender, EventArgs e)
        {
            _showLoginForm();
        }

        private void miList1Test_Click(object sender, EventArgs e)
        {
            /*var f = new frmList1User
            {
                MdiParent = this,
                //ListState = ListState.CanEdit,
            };
            f.Show();*/
            /*var f = new Form1
                        {
                            MdiParent = this,
                        };
            f.Show();*/
        }

        private void miMiniAll_Click(object sender, EventArgs e)
        {
            _miniAllForm();
        }

        private void miCloseAll_Click(object sender, EventArgs e)
        {
            _closeAllForm();
        }

        /*public void Show(Form form)
        {
            if (form.WindowState == FormWindowState.Minimized)
            {
                form.WindowState = FormWindowState.Normal;
            }
            form.BringToFront();
        }*/

        private void miAppSetting_Click(object sender, EventArgs e)
        {
            var f = new AppSettingForm();
            if (f.ShowDialog() == DialogResult.OK)
            {
            }
        }

        private void miListUser_Click(object sender, EventArgs e)
        {
            _showBrowseForm(typeof(UserBrowseForm), sender, true);
        }

        private void miChangePasswd_Click(object sender, EventArgs e)
        {
            var f = new UserSettingForm();
            //show ChangePasswd page
            if (f.ShowDialog() == DialogResult.OK)
            {
            }
        }


        #region private Helper

        private bool _showBrowseForm(Type formType, object sender, bool isSingleton = true)
        {
            var titleText = (sender is ToolStripItem) ? (sender as ToolStripItem).Text : string.Empty;

            if (isSingleton)
            {
                foreach (BrowseForm f in this.MdiChildren)
                {
                    if (f.GetType() == formType && f.TitleText == titleText)
                    {
                        if (f.WindowState == FormWindowState.Minimized)
                        {
                            f.WindowState = FormWindowState.Normal;
                        }
                        f.Visible = true;
                        f.Activate();
                        return true;
                    }
                }
            }
            try
            {
                var browseForm = (BrowseForm)Activator.CreateInstance(formType);
                browseForm.MdiParent = this;
                browseForm.TitleText = titleText;
                browseForm.Show();
                return true;
            }
            catch (Exception)
            {
                throw;
            }
        }

        private bool _showDialogForm()
        {
            return false;
        }

        private bool _showLoginForm()
        {
            _disableAllMenu();
            _closeAllForm();
            var f = new LoginForm("admin", "admin");
            if (f.ShowDialog() == DialogResult.OK)
            {
                _enableAllMenu();
                this.WindowState = FormWindowState.Maximized;
                return true;
            }
            else
            {
                _disableAllMenu();
                return false;
            }
        }

        private string GetTitleTextFromMenuItem(object sender)
        {
            return (sender is ToolStripItem) ? (sender as ToolStripItem).Text : string.Empty;
        }

        private void _enableAllMenu()
        {
            //
        }

        private void _disableAllMenu()
        {
            //
        }

        private void _closeAllForm()
        {
            foreach (Form f in this.MdiChildren)
            {
                f.Close();
            }
        }

        private void _miniAllForm()
        {
            foreach (Form f in this.MdiChildren)
            {
                f.WindowState = FormWindowState.Minimized;
            }
        }

        #endregion
    }
}
